<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 14-10-27
 * Time: 下午1:44
 * @author 郑钟良<zzl@ourstu.com>
 */

namespace Admin\Controller;


use Admin\Builder\AdminListBuilder;

class QuestionController extends AdminController
{

    /**问题列表页
     * @author 郑钟良<zzl@ourstu.com>
     */
    public function index($page = 1)
    {
        $questionList = D('Question/Question')->getList(array('is_repost' => 0), $totalCount, $page, 'id desc');
        $recommendIds = D('Question/QuestionRecommend')->getRecommendIds();

        foreach ($questionList as &$val) {
            if (in_array($val['id'], $recommendIds)) {
                $val['is_recommend'] = 1;
            } else {
                $val['is_recommend'] = 0;
            }
        }
        unset($val);
        $builder = new AdminListBuilder();
        $builder->meta_title = '问题列表';
        $builder->title('问题列表')->ajaxButton(U('admin/question/setRecommend'), array('recommend' => 1), '设为好问题')
            ->keyId()->keyUid('uid', '提问者')->keyText('title', '标题')->keyYesNo('is_recommend', '是否为好问题')->keyText('answer_num', '回答数')->keyText('share_num', '分享量')->keyText('content_s', '描述')->keyText('question_type', '问题类型')->keyTime('create_time', '提问时间')//->keyText('repost_num', '转发量')
            ->data($questionList)->pagination($totalCount, 50)->
            display();
    }

    /**好问题列表页
     * @author 郑钟良<zzl@ourstu.com>
     */
    public function recommend()
    {
        $questionList = D('Question/QuestionRecommend')->getQuestions();
        $builder = new AdminListBuilder();
        $builder->meta_title = '好问题列表';
        $builder->title('好问题列表')->ajaxButton(U('admin/question/setRecommend'), array('recommend' => 0), '移出好问题')
            ->keyId()->keyUid('uid', '提问者')->keyText('title', '标题')->keyText('answer_num', '回答数')->keyText('share_num', '分享量')->keyText('content_s', '描述')->keyText('question_type', '问题类型')->keyTime('create_time', '提问时间')//->keyText('repost_num', '转发量')
            ->data($questionList)->
            display();
    }

    public function setRecommend($ids = '', $recommend = 1)
    {
        if (!is_array($ids) || !count($ids)) {
            $this->error('请勾选要设置的项！');
        }
        $model = D('Question/QuestionRecommend');
        if ($recommend == 1) {
            $model->addRecommend($ids);
            send_good_question_mssage($ids);
        } else {
            $model->deleteRecommend($ids);
        }
        $this->success('设置成功', $_SERVER['HTTP_REFERER']);
    }
} 